const http = require('http')
const content = require('./data/index.json')

const server = http.createServer((request,response)=>{
    response.setHeader("content-type","text/html;charset=utf-8");
    let {pathname,searchParams} = new URL(request.url,'http://localhost:985');

    if(pathname === '/happy'){
        let str = '<h3>新闻列表页</h3><ul>';

        content.forEach((item)=>{
            str += `<li><a href='/now?id=${item.id}'>${item.newsTitle}</a></li>`;
        })
        str += '</ul>'
        
        response.end(str);
    }else if(pathname === '/now'){
        let id = searchParams.get('id');

        let obj = content.find((item)=>{
            return item.id === id;
        })

        let str = `
            <h3>${obj.newsTitle}</h3>
            <p>${obj.newsContent}</p>
        `;
        

        response.end(str)
    }else{
        response.end('您输入的地址有误，404')
    }
    
})

server.listen(985,()=>{
    console.log("启动成功了");
})

